import { createPinia } from "pinia";
import { useAiStore } from "./ai.js";
import { useAppStore } from "./app.js";
import { useAreaStore } from "./area.js";
import { useLoginStore } from "./login.js";
import { useComWinStore } from "./com-win.js";

export let store = {};

export default function (app) {
  app.use(createPinia());
  app.config.globalProperties.$comStore = store = {
    ai: useAiStore(),
    app: useAppStore(),
    area: useAreaStore(),
    login: useLoginStore(),
    comWin: useComWinStore(),
  };

  Object.keys(store).forEach((key) => {
    // 从缓存还原数据到store
    store[key].$state = {
      ...store[key].$state,
      ...JSON.parse(localStorage.getItem(`_store-${key}`)),
    };
    // 监听 store 变化进行缓存
    store[key].$subscribe((mutation, state) => {
      localStorage.setItem(`_store-${key}`, JSON.stringify(state));
    });
  });
}
